import com.Module.product.Product;

import common.fileupload.Fileupload1;
import common.fileupload.Fileupload3;
import common.multcombo.MultCombo;

import flash.events.Event;

import j2as3.collection.HashMap;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;
import mx.rpc.events.ResultEvent;
import mx.validators.Validator;

import spark.components.ComboBox;
import spark.components.FormItem;
import spark.components.TextInput;

var attrValueMap:j2as3.collection.HashMap = new HashMap;

//保存后,关闭.
private function saveHandler(event:ResultEvent):void {
	var state:Boolean = event.result.state;
	var msg:String = event.result.msg;
	if(state){
		Alert.show(msg+" ，如需为赋码物贴码，请进入码绑定管理。", "温馨提示");
		PopUpManager.removePopUp(this);
		this.dispatchEvent(new Event("afterSave"));
	}else {
		Alert.show(msg);
		b_save.enabled = true;
	//	b_submit.enabled = true;
	}
}
private function saveProduct():Boolean{
	var myValid:Array = Validator.validateAll([//校验表单.
		fromBean_nameValidateValid,
		fromBean_descValidateValid
	]);
	if(myValid.length == 0){
//		if(comboBox_attrGroup0){
//			if(comboBox_attrGroup0.selectedItem){
//				formBean.attrGroupId = comboBox_attrGroup0.selectedItem.id;
//			}else
//			{
//				Alert.show("请选属性组！","提示");
//				tabs.selectedIndex=0;
//				return false;
//			}
//			
//		}
		var categoryList:ArrayCollection = new ArrayCollection();
		if(categoryTree.treeSelectedItem==null)
		{
			Alert.show("请选择赋码物类型！","提示");
			//tabs.selectedIndex=0;
			return false;
		}
		categoryList.addItem(categoryTree.treeSelectedItem.id);
		formBean.categoryList = categoryList;
		if(categoryTree != null && categoryTree.treeSelectedItem != null){
			addAttr(categoryTree.treeSelectedItem.id);
		}
		if(attrValueMap != null){
			var keyColl:ArrayCollection = attrValueMap.keys;
			var attrValueList:ArrayCollection = new ArrayCollection;
			for(var i:int=0;i<keyColl.length;i++){
				var attrItemList:ArrayCollection = attrValueMap.getItem(keyColl.getItemAt(i) as String) as ArrayCollection;
				for(var j=0;j<attrItemList.length;j++){
					if(attrItemList.getItemAt(j).attrValue != null && attrItemList.getItemAt(j).attrValue != ""){
						attrValueList.addItem(attrItemList.getItemAt(j));
					}
				}
			}
			formBean.attrValueList = attrValueList;
		}
		if(approveState !=0){
			formBean.approveState = approveState;
		}
		remoteObject.save(formBean);
		return true;
		
	}else {
		//tabs.selectedIndex=0;
		return false;
	}
}
private function addAttr(itemId:String):void{
	var  itemInput:Object = null;
	var saveAttrItemList:ArrayCollection = new ArrayCollection;
	for(var i:int =0;i<attrItemList.length;i++){			
		var fieldType:String = attrItemList.getItemAt(i).type;
		if(fieldType == "3" || fieldType == "4"){
			itemInput=itemMap.getItem(attrItemList.getItemAt(i).id);
			attrItemList.getItemAt(i).attrValue = itemInput.newName;
			attrItemList.getItemAt(i).originalValue = itemInput.getFileName();
			if(itemInput.fileSize >0){
				attrItemList.getItemAt(i).attrValueSize = itemInput.fileSize;
			}
		}else if(fieldType == "6" ){
			itemInput=itemMap.getItem(attrItemList.getItemAt(i).id);
			attrItemList.getItemAt(i).attrValue = itemInput.selectedItem;
		}
		else if(fieldType == "2" ){
			itemInput=itemMap.getItem(attrItemList.getItemAt(i).id);
			var valueList:ArrayCollection = itemInput.getSelectValues();
			var attrValues:String = null;
			for(var j:int=0;j<valueList.length;j++){
				if(attrValues == null){
					attrValues = valueList.getItemAt(j) as String;
				}else{
					attrValues += ","+valueList.getItemAt(j);
				}
			}			
			attrItemList.getItemAt(i).attrValue = attrValues;
		}
		else{
			itemInput=itemMap.getItem(attrItemList.getItemAt(i).id);
			attrItemList.getItemAt(i).attrValue = itemInput.text;
		}
		attrItemList.getItemAt(i).productId = formBean.id;
		attrItemList.getItemAt(i).attrGroupId =itemId;
		saveAttrItemList.addItem(attrItemList.getItemAt(i));
		attrValueMap.put(itemId,saveAttrItemList);
	}
}
private function attrValuelistByGroupId(itemId:String):ArrayCollection
{
	attrItemList = attrValueMap.getItem(itemId) as ArrayCollection;
	return attrItemList;
	
}
private function createAttrValueForm(attrItemList:ArrayCollection){
	border_attrItem.removeAllElements();
	var  itemLabel:FormItem = null;
	var  itemInput = null;
	
	for(var i:int =0;i<attrItemList.length;i++){
		var obj:Object = attrItemList.getItemAt(i);
		var fieldType:String = obj.type;
		itemLabel = new FormItem();	
		if(fieldType =="1"){
			itemInput = new TextInput();
			itemInput.width=300;
			itemInput.text = obj.attrValue;			
			itemMap.put(obj.id,itemInput);	
		}else if(fieldType=="5"){
			itemInput = new spark.components.TextArea();
			itemInput.height=60;
			itemLabel.height=60;
			itemInput.width=300;
			itemInput.text = obj.attrValue;
			itemMap.put(obj.id,itemInput);	
		}else if(fieldType =="2"){//多选类型
			itemInput = new TextInput();
			itemInput.width=300;
			itemInput.text = obj.attrValue;			
			itemMap.put(obj.id,itemInput);	
		}else if(fieldType=="4"){
			itemInput = new Fileupload3();	
			itemInput.width=430;
			itemInput.filterFile="*.3gp;";
			itemInput.setFileName(obj.originalValue);
			itemInput.fileUrl =obj.attrValue; 
			itemInput.newName = obj.newAttrValue;
			itemMap.put(obj.id,itemInput);
		}else if(fieldType=="3"){
			itemInput = new Fileupload3();	
			itemInput.width=430;
			itemInput.filterFile="*.png;*.jpg;*.gif;";
			itemInput.setFileName(obj.originalValue);
			itemInput.fileUrl =obj.attrValue; 
			itemInput.newName = obj.newAttrValue;
			itemMap.put(obj.id,itemInput);
		}else if(fieldType=="6"){
			itemInput = new ComboBox();
			itemInput.width=300;
			var comboxValue:ArrayCollection = obj.comboxList as ArrayCollection;
			itemInput.dataProvider = comboxValue;
			if(obj.attrValue != null){
				var index :int = 0;
				for(var n:int =0;n<comboxValue.length;n++){
					var value:String = comboxValue.getItemAt(n) as String;
					var attrValue = obj.attrValue as String;
					if(value == attrValue){
						index = n ;
						//break;
					}
				}
				itemInput.selectedIndex=index;
			}
			itemMap.put(obj.id,itemInput);
		}else if(fieldType=="2"){
			itemInput = new MultCombo();
			itemInput.width=300;
			var comboValue:ArrayCollection = obj.comboxList as ArrayCollection;
			var comboArray:ArrayCollection = new ArrayCollection();
			for(var j=0;j<comboValue.length;j++){
				var comboMap:HashMap = new HashMap();
				comboMap.put("name",comboValue.getItemAt(j));
				comboMap.put("id",comboValue.getItemAt(j));
				comboArray.addItem(comboMap);
			}
			itemInput.dataProvider = comboArray;
			itemInput.valueField = "id";
			itemInput.labelField = "name";	
			if(obj.attrValue != null){
				var ss :String = obj.attrValue;	
				new function(multCombo:MultCombo,selValuesArr:String){//闭包
					multCombo.addEventListener(FlexEvent.CREATION_COMPLETE,function(e:FlexEvent){ 
						var valueArray:ArrayCollection = new ArrayCollection();
						var attrArray:Array = selValuesArr.split(",");
						for(var array:int =0;array<attrArray.length;array++){
							valueArray.addItem(attrArray[array]);
						}
						//Alert.show(valueArray+"||||"+multCombo.name+"||");
						multCombo.setSelectValues(valueArray);
					});
				}(itemInput,ss);
				
			}
			itemMap.put(obj.id,itemInput);
		}
		itemLabel.label = obj.name+":";					
		itemLabel.addElement(itemInput);			
		
		border_attrItem.addElement( itemLabel );
	}
}
private function createAttrValueForm1(attrItemList:ArrayCollection){
	border_attrItem.removeAllElements();
	var  itemLabel:FormItem = null;
	var  itemInput = null;
	
	for(var i:int =0;i<attrItemList.length;i++){
		var obj:Object = attrItemList.getItemAt(i);
		var fieldType:String = obj.type;
		itemLabel = new FormItem();	
		if(fieldType =="1"){
			itemInput = new TextInput();
			itemInput.width=300;
			itemInput.text = obj.attrValue;			
			itemMap.put(obj.id,itemInput);	
		}else if(fieldType=="5"){
			itemInput = new spark.components.TextArea();
			itemInput.height=60;
			itemLabel.height=60;
			itemInput.width=300;
			itemInput.text = obj.attrValue;
			itemMap.put(obj.id,itemInput);	
		}else if(fieldType =="2"){//多选类型
			itemInput = new TextInput();
			itemInput.width=300;
			itemInput.text = obj.attrValue;			
			itemMap.put(obj.id,itemInput);	
		}else if(fieldType=="6"){
			itemInput = new ComboBox();
			itemInput.width=300;
			var comboxValue:ArrayCollection = obj.comboxList as ArrayCollection;
			itemInput.dataProvider = comboxValue;
			if(obj.attrValue != null){
				var index :int = 0;
				for(var n:int =0;n<comboxValue.length;n++){
					var value:String = comboxValue.getItemAt(n) as String;
					var attrValue = obj.attrValue as String;
					if(value == attrValue){
						index = n ;
						//break;
					}
				}
				itemInput.selectedIndex=index;
			}
			itemMap.put(obj.id,itemInput);
		}else if(fieldType=="2"){
			itemInput = new MultCombo();
			itemInput.width=300;
			var comboValue:ArrayCollection = obj.comboxList as ArrayCollection;
			var comboArray:ArrayCollection = new ArrayCollection();
			for(var j=0;j<comboValue.length;j++){
				var comboMap:HashMap = new HashMap();
				comboMap.put("name",comboValue.getItemAt(j));
				comboMap.put("id",comboValue.getItemAt(j));
				comboArray.addItem(comboMap);
			}
			itemInput.dataProvider = comboArray;
			itemInput.valueField = "id";
			itemInput.labelField = "name";	
			if(obj.attrValue != null){
				var ss :String = obj.attrValue;	
				new function(multCombo:MultCombo,selValuesArr:String){//闭包
					multCombo.addEventListener(FlexEvent.CREATION_COMPLETE,function(e:FlexEvent){ 
						var valueArray:ArrayCollection = new ArrayCollection();
						var attrArray:Array = selValuesArr.split(",");
						for(var array:int =0;array<attrArray.length;array++){
							valueArray.addItem(attrArray[array]);
						}
						//Alert.show(valueArray+"||||"+multCombo.name+"||");
						multCombo.setSelectValues(valueArray);
					});
				}(itemInput,ss);
				
			}
			itemMap.put(obj.id,itemInput);
		}
		itemLabel.label = obj.name+":";					
		itemLabel.addElement(itemInput);			
		
		border_attrItem.addElement( itemLabel );
	}
}
private function selectTreeItem(itemId:String):void{
	if(itemId !=groupAttbid && groupAttbid != null){
		addAttr(groupAttbid);
	}
	attrItemList = attrValuelistByGroupId(itemId);
	if(attrItemList == null){
		remoteObject.listByGroupId(itemId,formBean.id);
	}else {
		createAttrValueForm(attrItemList);
	}
	groupAttbid=itemId;
}

private function listByGroupIdHandler(event:ResultEvent):void
{
	attrItemList = event.result as ArrayCollection;
	createAttrValueForm(attrItemList);
}